package com.yhl.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yhl.entity.Role;
import com.yhl.entity.User;
import com.yhl.mapper.RoleMapper;
import com.yhl.mapper.UserMapper;
import com.yhl.service.UserService;
import com.yhl.utils.DateUtils;
import lombok.val;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;


@Service
public class UserServiceImpl implements UserService {
    @Autowired
    UserMapper userMapper;


    @Override
    public void addUser(User user) {
        LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        userMapper.insert(user);
    }

    @Override
    public void deleteUserById(String id) {
        LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(User::getId,id);
        userMapper.delete(lambdaQueryWrapper);
    }

    @Override
    public void updateUser(User user) {
        userMapper.updateById(user);
    }

    @Override
    public User getUserById(int id) {
        User user = userMapper.selectById(id);
        Role role = roleMapper.selectById(user.getUserRole());
        user.setUserRoleName(role.getRoleName());
        return user;
    }

    @Override
    public User getUserByUserCode(String userCode) {
        /*QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("user_code",userCode);*/
        LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(User::getUserCode,userCode);
        List<User> userList = userMapper.selectList(lambdaQueryWrapper);
        if(userList.size()==0){
            return null;
        }else{
            return userList.get(0);
        }
    }

    @Autowired
    RoleMapper roleMapper;
    @Override
    public IPage<User> getUserByPage(User user) {
        IPage<User> page  = new Page<>();
        page.setCurrent(user.getCurrentPage());
        page.setSize(5);
        LambdaQueryWrapper<User>lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if(StringUtils.isNotEmpty(user.getUserName())){
            lambdaQueryWrapper.like(User::getUserName,user.getUserName());
        }
        if(user.getUserRole()!= 0){
            lambdaQueryWrapper.eq(User::getUserRole,user.getUserRole());
        }
        IPage<User> userIPage = userMapper.selectPage(page,lambdaQueryWrapper);
        for (User user1:userIPage.getRecords()) {
            Role role = roleMapper.selectById(user1.getUserRole());
            user1.setUserRoleName(role.getRoleName());
            String nowYear = DateUtils.format(new Date(),DateUtils.DATE_YEAE);
            String birthday = DateUtils.format(user1.getBirthday(),DateUtils.DATE_YEAE);
            int age = Integer.valueOf(nowYear)-Integer.valueOf(birthday);
            user1.setAge(age);
        }
        return userIPage;
    }
}
